//package com.example.grpcserver.service;
//
//import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
//import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
//
//import com.example.grpcserver.entity.AccountEntity;
//import com.example.grpcserver.exception.NotFoundException;
//import com.example.grpcserver.repository.AccountRepository;
//import io.grpc.stub.StreamObserver;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//import net.devh.boot.grpc.server.service.GrpcService;
//import org.example.grpcserver.Account;
//import org.example.grpcserver.AccountServiceGrpc;
//import org.example.grpcserver.BalanceSumResponse;
//import org.example.grpcserver.IdQuery;
//import org.example.grpcserver.IdRangeQuery;
//import org.springframework.beans.factory.annotation.Autowired;
//
///**
// * Create on: 2020/8/19.
// *
// * @author hongwei.zhang
// */
//@GrpcService
//public class AccountGrpcServerRefer extends AccountServiceGrpc.AccountServiceImplBase {
//  private AccountRepository accountRepository;
//
//  @Autowired
//  public AccountGrpcServerRefer(AccountRepository accountRepository) {
//    this.accountRepository = accountRepository;
//  }
//
//  @Override
//  public void getOne(IdQuery request, StreamObserver<Account> responseObserver) {
//    Optional<AccountEntity> opt = accountRepository.findById(request.getId());
//    if (opt.isPresent()) {
//      AccountEntity accountEntity = opt.get();
//      Account response = Account.newBuilder()
//          .setId(accountEntity.getId())
//          .setEmail(accountEntity.getEmail())
//          .setBalance(accountEntity.getBalance()).build();
//      responseObserver.onNext(response);
//    } else {
//      responseObserver.onError(new NotFoundException());
//    }
//    responseObserver.onCompleted();
//  }
//
//  /**
//   */
//  public StreamObserver<IdQuery> getMany(StreamObserver<Account> responseObserver) {
//    return new StreamObserver<IdQuery>() {
//      @Override
//      public void onNext(IdQuery idQuery) {
//        accountRepository.findById(idQuery.getId()).ifPresent(entity->{
//          Account account = Account.newBuilder()
//              .setId(entity.getId())
//              .setEmail(entity.getEmail())
//              .setBalance(entity.getBalance()).build();
//          responseObserver.onNext(account);
//        });
//      }
//
//      @Override
//      public void onError(Throwable throwable) {
//      }
//
//      @Override
//      public void onCompleted() {
//        responseObserver.onCompleted();
//      }
//    };
//  }
//
//  /**
//   */
//  public void getByRange(IdRangeQuery request, StreamObserver<Account> responseObserver) {
//      List<AccountEntity> entities = (List<AccountEntity>) accountRepository.findAll();
//      entities = entities.stream()
//          .filter(entity ->
//              (entity.getId()>=request.getLower() && entity.getId()<=request.getUpper()))
//          .collect(Collectors.toList());
//      for (AccountEntity entity: entities) {
//        responseObserver.onNext(Account.newBuilder()
//            .setId(entity.getId())
//            .setEmail(entity.getEmail())
//            .setBalance(entity.getBalance()).build());
//      }
//      responseObserver.onCompleted();
//  }
//
//  /**
//   */
//  public StreamObserver<IdQuery> calBalanceSum(StreamObserver<BalanceSumResponse> responseObserver) {
//    return new StreamObserver<IdQuery>() {
//      private double sum = 0;
//      private List<Long> ids = new ArrayList<>();
//      @Override
//      public void onNext(IdQuery idQuery) {
//        AccountEntity account = accountRepository.findById(idQuery.getId()).orElseThrow(NotFoundException::new);
//        sum += account.getBalance();
//        ids.add(account.getId());
//      }
//
//      @Override
//      public void onError(Throwable throwable) {
//      }
//
//      @Override
//      public void onCompleted() {
//        BalanceSumResponse.Builder builder = BalanceSumResponse.newBuilder().setBalanceSum(sum);
//        for (Long id : ids) {
//          builder.addIds(id);
//        }
//        responseObserver.onNext(builder.build());
//        responseObserver.onCompleted();
//      }
//    };
//  }
//}
